//
//  TQQListTableCell.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2024/1/22.
//

import UIKit

class TQQListTableCell: UITableViewCell {
    var titleLabel:UILabel?
    var subLabel:UILabel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    func initUI(){
//rowHeight=50
        titleLabel = UILabel(frame: CGRect(x: 15, y: 5, width: screen_W-30, height: 25))
        if let titleLabel = titleLabel{
            contentView.addSubview(titleLabel)
        }
        subLabel = UILabel(frame: CGRect(x: 15, y: 35, width: screen_W-30, height: 15))
        if let subLabel = subLabel{
            contentView.addSubview(subLabel)
            subLabel.font = UIFont(name: "Avenir-Light", size: 10)
            subLabel.textColor = UIColor.secondaryLabel
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setHighlighted(_ highlighted:Bool,animated:Bool){
        super.setHighlighted(highlighted, animated: animated)
//高亮和未高亮时展示cell动画
        if (self.isHighlighted){
            let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
            scaleAnimation.duration = 0.25;
            scaleAnimation.fromValue = 1.0
            scaleAnimation.toValue = 0.95
            self.titleLabel?.layer.add(scaleAnimation, forKey: "title.scaleXY")
        }else{
            let sprintSacle = CASpringAnimation(keyPath: "transform.scale")
            sprintSacle.duration = 0.25 //估算时间 返回弹簧动画到停止时的估算时间，根据当前的动画参数估算；
            sprintSacle.fromValue = 0.95
            sprintSacle.toValue = 1.0
            sprintSacle.mass = 1 //模拟的是质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大 默认值：1
            sprintSacle.stiffness = 20 //刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快。默认值： 100 ；
            sprintSacle.damping = 0.1 //阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快。默认值：10；数值越大,弹性越不明显
            sprintSacle.initialVelocity = 2 //初始速率
            self.titleLabel?.layer.add(sprintSacle, forKey: "title.scaleXY")
        }
    }
    
    func updateContent(modelInfo:TQQListModel,at indexPath:IndexPath){
        self.titleLabel?.attributedText = modelInfo.titleString
        self.subLabel?.text = modelInfo.targetVC
        
        if (0 == (indexPath.row % 2)) {
            self.backgroundColor = .secondarySystemBackground
        } else {
            self.backgroundColor = .systemBackground
        }
         
    }
    
    static func createTableViewCellWithTableView(tableView:UITableView )->TQQListTableCell{
        let identifier = "TQQListCell";
        if let cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TQQListTableCell{
            return cell
        }else{
            return TQQListTableCell(style: .default, reuseIdentifier: identifier)
        }
    }

}
